import numpy as np
import re
import pandas as pd
import glob


def clean():
    pattern = re.compile(r'\d+')  # 查找数字
    file = pd.read_csv('books.csv')
    rows = np.array(file)
    names = []
    talk = []
    # talklist = []
    for row in rows:
        if row[2] != "目前无人评价":
            names.append(row[0])
            n = pattern.findall(row[2])
            talk.extend(n)
    talklist = pd.DataFrame({'姓名': names, '评论数': talk})
    talk1 = talklist.dropna(axis=0, how='any')
    talk1.to_csv('talk.csv', index=None, encoding='utf-8')


def sum():
    df = pd.read_csv('talk.csv')
    df_sum = df.groupby('姓名')['评论数'].sum()
    df_sum.to_csv('../../../Data/zhangjinyang/sum/talk_sum.csv', encoding='utf-8')


# 合并几个分类的csv文件
def mergeAll():
    # 获取文件名列表
    file_list = glob.glob('../../../Data/zhangjinyang/books_*.csv')

    # 读取每个文件并保存到DataFrame列表中
    df_list = []
    for i, file_name in enumerate(file_list):
        df = pd.read_csv(file_name)
        df_list.append(df)

    # 按行合并多个DataFrame
    df_merged = pd.concat(df_list, axis=0, ignore_index=True)
    df_merged.to_csv('books.csv', index=None, encoding='utf-8')


if __name__ == '__main__':
    mergeAll()
    clean()
    sum()
